<template>
  <div class="dashboard-container" style="width: 100%;padding:0;margin:0;">
    <!-- 顶部标题栏 -->
    <el-row class="header-row">
      <el-col :span="24" class="header-content">
        <h1>财务管理</h1>
        <div class="header-info">
        </div>
      </el-col>
    </el-row>

    <!-- 指标卡片 -->

    <!-- 支出 -->
    <h1 class="font-bold ml-2.5 text-purple-500" style="font-size:1.5rem"><i class="el-icon-money"></i> 支出</h1>
    <!--编辑支出模态框。-->
    <el-dialog title="编辑支出" :visible.sync="edit_exp_dialog_visible">
      <el-form :model="edit_exp_form" @keyup.enter.native="edit_expense">
        <el-form-item label="支出日期" :label-width="formLabelWidth">
          <el-date-picker
              type="date"
              value-format="yyyy-MM-dd"
              v-model="edit_exp_form.date"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类" :label-width="formLabelWidth">
          <el-select
              v-model="edit_exp_form.first_cate_id"
              placeholder="支出一级分类"
              filterable
              @change="exp_first_cate_change"
          >
            <el-option v-for="item in exp_first_cate_list" :key="item.first_cate_id" :label="item.first_cate_name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" :label-width="formLabelWidth">
          <el-select
              v-model="edit_exp_form.second_cate_id"
              placeholder="支出二级分类"
              filterable
          >
            <el-option v-for="item in exp_second_cate_options" :key="item.id" :label="item.second_cate_name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支出金额" :label-width="formLabelWidth">
          <el-input placeholder="请输入该笔支出金额" v-model="edit_exp_form.amount"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="edit_exp_form.note"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="edit_exp_dialog_visible = false">取 消</el-button>
        <el-button type="primary" @click="edit_expense">确 定</el-button>
      </div>
    </el-dialog>
    <!--概况-->
    <el-row :gutter="8" class="kpi-row">
      <el-col :span="6" v-for="(item, index) in ExpenseCardList" :key="index" class="kpi-card">

        <el-card :body-style="{ padding: '15px' }" style="margin-top: 1rem">
          <div class="kpi-title">{{ item.name }}</div>
          <div class="kpi-value" :title="item.origin_value">

            {{ item.value }}
          </div>
          <div class="kpi-trend" v-if="item.trend" :class="{ up: item.trend > 0, down: item.trend < 0 }">
            <i :class="item.trend > 0 ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
            {{ Math.abs(item.trend) }}%
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="15" class="chart-row" style="margin-top: 1rem">
      <!--支出结构-->
      <el-col :span="8" class="chart-card" style="height: 500px">
        <el-card :body-style="{ padding: '15px' }">
          <div slot="header" class="chart-header">
            <span><i class="fa fa-pie-chart"></i> 支出结构</span>
            <!--          选择日期-->
            <el-date-picker
                style="margin-bottom: 1rem"
                placeholder="选择支出日期"
                v-model="exp_date"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                type="date"
            ></el-date-picker>
          </div>
          <div style="height: 400px">
            <PieChartComponent :title="'支出结构'" :data="exp_struct"></PieChartComponent>
          </div>
        </el-card>
      </el-col>
      <!-- 支出表格 -->
      <el-col :span="16" class="chart-card" style="height: 500px">

        <el-card :body-style="{ padding: '15px' }">
          <div slot="header" class="chart-header">
            <span><i class="fa fa-table"></i> 支出明细</span>
            <el-button
                type="primary"
                @click="exp_add_dialog_visible = true"
            >
              新增支出
            </el-button>
          </div>

          <div>
            <el-table
                height="360"
                border
                :data="exp_data"
                style="width: 100%"
                class="ml-5"
            >
              <el-table-column
                  prop="date"
                  label="支出日期"
                  align="center">
              </el-table-column>
              <el-table-column
                  prop="first_cate_name"
                  align="center"
                  label="支出一级分类">
              </el-table-column>
              <el-table-column
                  prop="second_cate_name"
                  align="center"
                  label="支出二级分类">
              </el-table-column>
              <el-table-column
                  prop="amount"
                  align="center"
                  label="支出金额"
                  sortable>
              </el-table-column>
              <el-table-column
                  prop="note"
                  label="备注"
                  align="center">
              </el-table-column>
              <el-table-column
                  prop="attachment"
                  align="center"
                  label="附件">
              </el-table-column>
              <el-table-column
                  prop="execute"
                  label="操作"
                  align="center">
                <template #default="scope">
                  <!-- 编辑按钮，使用编辑图标 -->
                  <el-button
                      size="small"
                      type="primary"
                      icon="el-icon-edit"
                      @click="handleEdit(scope.row)">
                  </el-button>
                  <!-- 删除按钮，使用删除图标 -->
                  <el-popconfirm
                      style="margin-left: 1rem"
                      confirm-button-text='确定'
                      cancel-button-text='取消'
                      icon="el-icon-info"
                      icon-color="red"
                      title="确定删除此分配方案吗？"
                      @confirm="handleDelete(scope.row.id)"
                  >
                    <el-button
                        size="small"
                        type="danger"
                        icon="el-icon-delete"
                        slot="reference"
                    >
                    </el-button>
                  </el-popconfirm>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <div class="pagination-container">
              <el-pagination
                  @size-change="exp_size_change"
                  @current-change="exp_current_change"
                  :current-page="ExpCurrentPage"
                  :page-sizes="[5, 20, 30, 40]"
                  :page-size="ExpPageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="ExpTotalCount"
              ></el-pagination>
            </div>

          </div>

        </el-card>
        <el-dialog title="新增支出" :visible.sync="exp_add_dialog_visible">
          <el-form :model="add_exp_form" @keyup.enter.native="add_expense">
            <el-form-item label="支出日期" :label-width="formLabelWidth">
              <el-date-picker
                  type="date"
                  value-format="yyyy-MM-dd"
                  v-model="add_exp_form.date"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="一级分类" :label-width="formLabelWidth">
              <el-select
                  v-model="add_exp_form.first_cate_id"
                  placeholder="支出一级分类"
                  filterable
                  @change="exp_first_cate_change"
              >
                <el-option v-for="item in exp_first_cate_list" :key="item.id" :label="item.first_cate_name"
                           :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="二级分类" :label-width="formLabelWidth">
              <el-select
                  v-model="add_exp_form.second_cate_id"
                  placeholder="支出二级分类"
                  filterable
              >
                <el-option v-for="item in exp_second_cate_options" :key="item.id" :label="item.second_cate_name"
                           :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="支出金额" :label-width="formLabelWidth">
              <el-input placeholder="请输入该笔支出金额" v-model="add_exp_form.amount"></el-input>
            </el-form-item>
            <el-form-item label="备注" :label-width="formLabelWidth">
              <el-input v-model="add_exp_form.note"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="exp_add_dialog_visible = false">取 消</el-button>
            <el-button type="primary" @click="add_expense">确 定</el-button>
          </div>
        </el-dialog>

      </el-col>
    </el-row>

    <el-row :gutter="15" class="chart-row" style="margin-top: 1rem">
      <!--支出结构-->
      <el-col :span="24" class="chart-card" style="height: 500px">
        <el-card :body-style="{ padding: '15px' }">
          <div slot="header" class="chart-header">
            <span><i class="fa fa-line-chart"></i> 支出趋势</span>
          </div>
          <div style="height: 400px">
            <TechLineChart :title="'当月支出趋势'"
                           :chart-data="exp_line_data"></TechLineChart>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 收入 -->
    <h1 class="font-bold ml-2.5 text-purple-500" style="font-size:1.5rem"><i class="el-icon-money"></i> 收入</h1>
    <!--编辑收入模态框。-->
    <el-dialog title="编辑收入" :visible.sync="edit_inc_dialog_visible">
      <el-form :model="edit_inc_form" @keyup.enter.native="edit_inc">
        <el-form-item label="收入日期" :label-width="formLabelWidth">
          <el-date-picker
              type="date"
              value-format="yyyy-MM-dd"
              v-model="edit_inc_form.date"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="收入一级分类" :label-width="formLabelWidth">
          <el-select
              v-model="edit_inc_form.first_cate_id"
              placeholder="支出一级分类"
              filterable
              @change="inc_first_cate_change"
          >
            <el-option v-for="item in inc_first_cate_list" :key="item.id" :label="item.first_cate_name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" :label-width="formLabelWidth">
          <el-select
              v-model="edit_inc_form.second_cate_id"
              placeholder="收入二级分类"
              filterable
          >
            <el-option v-for="item in inc_second_cate_options" :key="item.id" :label="item.second_cate_name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收入金额" :label-width="formLabelWidth">
          <el-input placeholder="请输入该笔收入金额" v-model="edit_inc_form.amount"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="edit_inc_form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="edit_inc_dialog_visible = false">取 消</el-button>
        <el-button type="primary" @click="edit_inc">确 定</el-button>
      </div>
    </el-dialog>
    <!--概况-->
    <el-row :gutter="8" class="kpi-row">
      <el-col :span="12" v-for="(item, index) in IncomeCardList" :key="index" class="kpi-card">

        <el-card :body-style="{ padding: '15px' }" style="margin-top: 1rem">
          <div class="kpi-title">{{ item.name }}</div>
          <div class="kpi-value" :title="item.origin_value">

            {{ item.value }}
          </div>
          <div class="kpi-trend" v-if="item.trend" :class="{ up: item.trend > 0, down: item.trend < 0 }">
            <i :class="item.trend > 0 ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
            {{ Math.abs(item.trend) }}%
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="15" class="chart-row" style="margin-top: 1rem">
      <!--收入结构-->
      <el-col :span="8" class="chart-card" style="height: 500px">
        <el-card :body-style="{ padding: '15px' }">
          <div slot="header" class="chart-header">
            <span><i class="fa fa-pie-chart"></i> 收入结构</span>
          </div>
          <div style="height: 400px">
            <PieChartComponent :title="'当月收入结构'" :data.sync="inc_struct"></PieChartComponent>
          </div>
        </el-card>
      </el-col>
      <!-- 收入表格 -->
      <el-col :span="16" class="chart-card" style="height: 500px">

        <el-card :body-style="{ padding: '15px' }">
          <div slot="header" class="chart-header">
            <span><i class="fa fa-table"></i> 收入明细</span>
            <el-button
                type="primary"
                @click="inc_add_dialog_visible = true"
            >
              新增收入
            </el-button>
          </div>
          <!--          选择日期-->
          <el-date-picker
              style="margin-bottom: 1rem"
              placeholder="选择收入日期"
              v-model="inc_date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              type="date"
          ></el-date-picker>
          <div>
            <el-table
                height="290"
                border
                :data="inc_data"
                style="width: 100%"
                class="ml-5"
            >
              <el-table-column
                  prop="date"
                  label="收入日期"
                  align="center">
              </el-table-column>
              <el-table-column
                  prop="first_cate_name"
                  align="center"
                  label="收入一级分类">
              </el-table-column>
              <el-table-column
                  prop="second_cate_name"
                  align="center"
                  label="收入二级分类">
              </el-table-column>
              <el-table-column
                  prop="amount"
                  align="center"
                  label="收入金额"
                  sortable>
              </el-table-column>
              <el-table-column
                  prop="remark"
                  label="备注"
                  align="center">
              </el-table-column>
              <el-table-column
                  prop="execute"
                  label="操作"
                  align="center">
                <template #default="scope">
                  <!-- 编辑按钮，使用编辑图标 -->
                  <el-button
                      size="small"
                      type="primary"
                      icon="el-icon-edit"
                      @click="handle_inc_edit(scope.row)">
                  </el-button>
                  <!-- 删除按钮，使用删除图标 -->
                  <el-popconfirm
                      style="margin-left: 1rem"
                      confirm-button-text='确定'
                      cancel-button-text='取消'
                      icon="el-icon-info"
                      icon-color="red"
                      title="确定删除此分配方案吗？"
                      @confirm="handle_inc_delete(scope.row.id)"
                  >
                    <el-button
                        size="small"
                        type="danger"
                        icon="el-icon-delete"
                        slot="reference"
                    >
                    </el-button>
                  </el-popconfirm>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <div class="pagination-container">
              <el-pagination
                  @size-change="inc_size_change"
                  @current-change="inc_current_change"
                  :current-page="IncCurrentPage"
                  :page-sizes="[5, 20, 30, 40]"
                  :page-size="IncPageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="IncTotalCount"
              ></el-pagination>
            </div>

          </div>

        </el-card>
        <!--新增收入记录模态框-->
        <el-dialog title="新增收入" :visible.sync="inc_add_dialog_visible">
          <el-form :model="add_inc_form" @keyup.enter.native="add_income">
            <el-form-item label="收入日期" :label-width="formLabelWidth">
              <el-date-picker
                  type="date"
                  value-format="yyyy-MM-dd"
                  v-model="add_inc_form.date"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="收入一级分类" :label-width="formLabelWidth">
              <el-select
                  v-model="add_inc_form.first_cate_id"
                  placeholder="收入一级分类"
                  filterable
                  @change="inc_first_cate_change"
              >
                <el-option v-for="item in inc_first_cate_list" :key="item.id" :label="item.first_cate_name"
                           :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="收入二级分类" :label-width="formLabelWidth">
              <el-select
                  v-model="add_inc_form.second_cate_id"
                  placeholder="支出二级分类"
                  filterable
                  clearable
              >
                <el-option v-for="item in inc_second_cate_options" :key="item.id" :label="item.second_cate_name"
                           :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="收入金额" :label-width="formLabelWidth">
              <el-input placeholder="请输入该笔收入金额" v-model="add_inc_form.amount"></el-input>
            </el-form-item>
            <el-form-item label="备注" :label-width="formLabelWidth">
              <el-input placeholder="有什么需要备注的？" v-model="add_inc_form.note"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="inc_add_dialog_visible = false">取 消</el-button>
            <el-button type="primary" @click="add_income">确 定</el-button>
          </div>
        </el-dialog>

      </el-col>
    </el-row>

  </div>
</template>

<script>
// import axios from 'axios'

import PieChartComponent from "@/components/ChartComponent/PieChartComponent.vue";
import TechLineChart from "@/components/ChartComponent/LineChartComponent.vue";
import moment from 'moment';

export default {
  computed: {},
  components: {TechLineChart, PieChartComponent},
  name: 'FinanceView',

  data() {

    return {

      // 表格当前页显示的数据
      exp_data: [],
      // 当前页码
      ExpCurrentPage: 1,
      // 每页显示条数
      ExpPageSize: 5,
      // 总数据条数
      ExpTotalCount: 0,

      // 支出指标卡数据
      ExpenseCardList: [
        {name: "当年累计支出", value: "100万"},
        {name: "当月累计支出", value: "12万"},
      ],
      // 当日支出明细
      all_exp: [],
      // 支出时间选择
      exp_date: moment(new Date()).format('YYYY-MM-DD'),
      // 支出结构数据。
      exp_struct: [],
      IncomeCardList: [
        {name: "当年累计收入", value: "100万"},
        {name: "当月累计收入", value: "12万"},
      ],

      // 支出曲线图
      exp_line_data: {
        xAxis: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        series: [
          {name: '数据A', data: [120, 132, 101, 134, 90, 230, 210, 230, 190, 230, 210, 170]},
        ]
      },

      // 新增支出弹窗表单组件配置项
      exp_add_dialog_visible: false,

      // 新增支出表单
      add_exp_form: {
        user_id: 1,
        // 默认是今天
        date: moment(new Date()).format('YYYY-MM-DD')
      },
      formLabelWidth: '120px',
      // 编辑支出表单模态框
      edit_exp_dialog_visible: false,
      // 编辑支出表单
      edit_exp_form: {},

      // 所有一级分类列表
      exp_first_cate_list: [],
      // 基于一级分类筛选的可选的二级分类
      exp_second_cate_options: [],

      // 所有一级分类列表
      inc_first_cate_list: [],
      // 基于一级分类筛选的可选的二级分类
      inc_second_cate_options: [],

      // 新增收入弹窗表单组件配置项
      inc_add_dialog_visible: false,
      add_inc_form: {},
      // 当日收入明细
      all_inc: [],
      // 收入时间选择
      inc_date: moment(new Date()).format('YYYY-MM-DD'),
      inc_data: [],
      // 当前页码
      IncCurrentPage: 1,
      // 每页显示条数
      IncPageSize: 5,
      // 总数据条数
      IncTotalCount: 0,
      // 收入
      edit_inc_form: {},
      edit_inc_dialog_visible: false,
      inc_struct: [],



    }
  },
  created() {
    this.call_inc_card_data()
    this.call_inc_structure_data()
  },
  mounted() {
    this.call_exp_card_data()
    this.call_exp_struct_data()
    this.call_daily_exp_data()
    this.call_exp_all_first_cate()
    this.call_inc_all_fist_cate()
    this.call_daily_inc_data()
  },
  beforeDestroy() {
  },
  methods: {
    // 格式化数据
    formatData(value) {
      // 处理超过万的数值，保留两位小数并显示为“x.xx万”
      if (value >= 10000) {
        return (value / 10000).toFixed(2) + '万';
      } else if (value > 1) {
        return value.toFixed(2)
      }
      // 处理小数（这里假设小于1的数值是比例值，需要转成百分比）
      else if (value > 0 && value < 1) {
        return (value * 100).toFixed(2) + '%';
      }
      // 其他情况（如整数、不符合上述条件的小数等），直接返回原值
      else {
        return value
      }
    },

    // 支出卡片累计数据
    call_exp_card_data() {

      // 然后用这个instance发送请求
      this.$axios.post('/exp/statistics', {
        year: this.exp_date,
        month: this.exp_date,
        user_id: 1
      })
          .then(response => {
            this.ExpenseCardList = response.data.data.map(item => ({
              ...item,
              value: this.formatData(item.value)
            }))
          })
    },

    // 支出结构数据
    call_exp_struct_data() {
      this.$axios.post('/exp/exp_struct', {
        user_id: 1,
        start_date: this.exp_date
      })
          .then(response => {
            this.exp_struct = response.data.data.map(item => ({
              ...item,
              value: this.formatData(item.value)
            }))
          })

    },
    // 某日支出明细数据。
    call_daily_exp_data() {
      // 然后用这个instance发送请求
      this.$axios.post('/exp/date_exp', {
        user_id: 1,
        date: this.exp_date
      })
          .then(response => {
            this.all_exp = response.data.data.map(item => ({
              ...item
            }))
            this.exp_pg_change()
            this.ExpTotalCount = this.all_exp.length
            console.log(this.all_exp)

          })
          .catch(error => {
            console.error(error);
          });
    },

    // 某月支出数据
    call_month_exp_data() {
      // 然后用这个instance发送请求
      this.$axios.post('/exp/month_exp', {
        user_id: 1,
        date: this.exp_date
      })

          .then(response => {
            if (response.data.data.avg_amount !== 0.0) {
              this.exp_line_data.series = response.data.data.y_axis
              this.exp_line_data.xAxis = response.data.data.x_axis

              // 数据分析值
              this.ExpenseCardList.push({name: "月支出标准差", value: this.formatData(response.data.data.std_dev)})
              this.ExpenseCardList.push({name: "支出均值", value: this.formatData(response.data.data.avg_amount)})
            }

          })
          .catch(error => {
            console.error(error);
          });
    },

    // 处理每页条数变化
    exp_size_change(val) {
      this.ExpPageSize = val;
      // 重置为第一页
      this.ExpCurrentPage = 1;
      this.exp_pg_change();
    },

    // 处理页码变化
    exp_current_change(val) {
      this.ExpCurrentPage = val;
      this.exp_pg_change();
    },

    // 处理分页逻辑
    exp_pg_change() {
      // 计算起始索引
      const startIndex = (this.ExpCurrentPage - 1) * this.ExpPageSize;
      // 计算结束索引
      const endIndex = startIndex + this.ExpPageSize;
      // 截取数据，实现前端分页
      this.exp_data = this.all_exp.slice(startIndex, endIndex);
    },

    // [支出]获取所有的一级分类
    call_exp_all_first_cate() {
      this.$axios.get('/exp/first_cate')
          .then(response => {
            this.exp_first_cate_list = response.data.data.map(item => ({
              ...item,
            }))
          })
    },

    // [支出]根据一级分类id筛选可选择的二级分类
    call_exp_sec_cate_by_id(first_cate_id) {
      this.$axios.post('/exp/get_second_cate_by_first_cate_id', {
        first_cate_id: first_cate_id
      }).then(response => {
        this.exp_second_cate_options = response.data.data.map(item => ({
          ...item,
        }))
      })
    },

    // 【支出】一级分类选择好时，自动加载可选的二级分类。
    exp_first_cate_change(newVal) {
      console.log("新一级分类id",newVal)
      this.call_exp_sec_cate_by_id(newVal)
    },

    //【支出】打开编辑界面
    handleEdit(row) {
      this.edit_exp_dialog_visible = true;
      this.edit_exp_form = row
    },

    // 【支出】提交编辑表单。
    async edit_expense() {
      try {
        const res = await this.$axios.post('/exp/edit_exp', this.edit_exp_form);
        if (res.data.code === 200) {
          this.$message({
            type: 'success',
            message: '更新成功。'
          });

        } else if (res.data.code === 400) {
          this.$message({
            type: 'error',
            message: '更新失败' + res.data.message
          });
        }
      } catch (error) {
        // 可以在这里处理请求失败的情况（例如网络错误、后端返回错误等）
        console.error('更新失败：', error);
        this.$message({
          type: 'error',
          message: '更新失败，请重试。'
        });
      }
    },

    // 【支出】根据id删除支出
    handleDelete(id) {
      this.$axios.post('/exp/delete_exp', {
        exp_id: id
      }).then(res => {
        if (res.data.code === 200) {
          this.$message({
            type: 'success',
            message: "删除成功。"
          })
          window.location.reload()
        } else if (res.data.code === 400) {
          this.$message({
            type: 'error',
            message: res.data.message
          })
        }
      })
    },
    // 新增支出
    add_expense() {
      this.$axios.post('/exp/add', this.add_exp_form)
          .then(response => {
            if (response.data.code === 200) {
              this.$notify({
                type: 'success',
                title: '新增成功',
                message: '本次提交已完成。'
              })
              this.call_exp_card_data()
              this.call_exp_struct_data()
              this.call_daily_exp_data()
            }
          })
    },

    // 收入卡片累计数据
    call_inc_card_data() {
      this.$axios.post('/inc/get_card', {
        date: this.inc_date
      })
          .then(response => {
            this.IncomeCardList = response.data.data.map(item => ({
              ...item,
              value: this.formatData(item.value)
            }))
          })
    },

    // 某日收入明细数据。
    call_daily_inc_data() {
      // 然后用这个instance发送请求
      this.$axios.post('/inc/date_inc', {
        date: this.inc_date
      })
          .then(response => {
            this.all_inc = response.data.data.map(item => ({
              ...item
            }))
            this.inc_pg_change()
            this.IncTotalCount = this.all_inc.length
          })
          .catch(error => {
            console.error(error);
          });
    },

    // 新增收入
    add_income() {
      this.$axios.post('/inc/add', this.add_inc_form)
          .then(response => {
            if (response.data.code === 200) {
              this.$notify({
                type: 'success',
                title: '新增成功',
                message: '本次提交已完成。'
              })
              this.call_exp_card_data()
              this.call_exp_struct_data()
              this.call_daily_exp_data()
            }
          })
    },


    // 处理每页条数变化
    inc_size_change(val) {
      this.IncPageSize = val;
      // 重置为第一页
      this.IncCurrentPage = 1;
      this.exp_pg_change();
    },

    // 处理页码变化
    inc_current_change(val) {
      this.IncCurrentPage = val;
      this.exp_pg_change();
    },

    // 处理分页逻辑
    inc_pg_change() {
      // 计算起始索引
      const startIndex = (this.IncCurrentPage - 1) * this.IncPageSize;
      // 计算结束索引
      const endIndex = startIndex + this.IncPageSize;
      // 截取数据，实现前端分页
      this.inc_data = this.all_inc.slice(startIndex, endIndex);
    },

    // 根据以及分类id请求二级收入分类列表。
    call_inc_sec_date_by_id(first_cate_id) {
      this.$axios.post('/inc/get_second_cate_by_first_cate_id', {
        first_cate_id: first_cate_id
      }).then(res => {
        if (res.data.code === 200) {
          this.inc_second_cate_options = res.data.data
        }
      })
    },

    //[收入]获取所有的一级分类
    call_inc_all_fist_cate() {
      this.$axios.get('/inc/get_all_first_cates').then(res => {
        this.inc_first_cate_list = res.data.data
      })
    },

    // 根据收入一级分类请求可选的收入二级分类
    inc_first_cate_change(new_inc_cate_id) {
      console.log(this.inc_first_cate_list)
      this.call_inc_sec_date_by_id(new_inc_cate_id)
    },

    // 打开编辑收入模态框
    handle_inc_edit(row) {
      this.edit_inc_dialog_visible = true
      this.edit_inc_form = row
    },

    // 提交编辑后的收入
    edit_inc() {
      this.$axios.post('/inc/update_by_id', this.edit_inc_form).then(res => {
        if (res.data.code === 200) {
          this.$message({
            type: 'success',
            message: '编辑成功，请刷新查看。'
          })
        } else if (res.data.code === 400) {
          this.$message({
            type: 'error',
            message: res.data.message
          })
        }
      })
    },
    // 删除收入记录。
    handle_inc_delete(record_id) {
      this.$axios.post('/inc/del_by_id', {
        record_id: record_id
      }).then(res => {
        if (res.data.code === 200) {
          this.$message({
            type: 'success',
            message: "删除成功"
          })
          this.call_daily_inc_data()
        } else if (res.data.code === 400) {
          this.$message({
            type: 'error',
            message: res.data.message
          })
        }
      })
    },
    // 收入结构数据
    call_inc_structure_data() {
      this.$axios.post('/inc/get_inc_structure', {
        date: this.inc_date
      })
          .then(response => {
            this.inc_struct = response.data.data.map(item => ({
              ...item,
              value: this.formatData(item.value)
            }))
          })

    },


  },
  watch: {
    exp_date: {
      deep: true,
      immediate: true,
      handler() {
        this.call_daily_exp_data()
        this.call_exp_card_data()
        this.call_exp_struct_data()
        this.call_month_exp_data()
      }
    },
    inc_date: {
      deep: true,
      immediate: true,
      handler() {
        this.call_daily_inc_data()
        this.call_inc_card_data()
        this.call_inc_structure_data()
      }
    },
  }


}
</script>

<style scoped>
.dashboard-container {
  padding: 20px;
  background-color: #f9f9f9;
  min-height: 100vh;
}

.header-row {
  margin-bottom: 20px;
}

.header-content {
  background-color: #fff;
  padding: 15px 20px;
  border-radius: 6px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-content h1 {
  margin: 0;
  font-size: 24px;
  color: #333;
  font-weight: 600;
}

.header-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.update-time {
  color: #666;
  font-size: 14px;
}

.kpi-row {
  margin-bottom: 20px;
}

.kpi-card {
  height: 100%;
}

.kpi-card .el-card {
  height: 100%;
  background-color: #fff;
  border-radius: 6px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.kpi-card .el-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.kpi-title {
  color: #666;
  font-size: 14px;
  margin-bottom: 10px;
}

.kpi-value {
  font-size: 22px;
  font-weight: 600;
  color: #333;
  margin-bottom: 5px;
}

.kpi-trend {
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.kpi-trend.up {
  color: #e74c3c;

}

.kpi-trend.down {
  color: #42b983;

}

.chart-row {
  margin-bottom: 20px;
}

.chart-card {
  height: 100%;
}

.chart-card .el-card {
  height: 100%;
  background-color: #fff;
  border-radius: 6px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  color: #333;
}

.chart-container {
  width: 100%;
  height: calc(100% - 44px);
  min-height: 300px;
}

.chart {
  width: 100%;
  height: 100%;
}

/* 响应式调整 */
@media (max-width: 1200px) {
  .kpi-card {
    flex: 0 0 33.333333%;
    max-width: 33.333333%;
  }

  .chart-card {
    flex: 0 0 50%;
    max-width: 50%;
  }
}

@media (max-width: 768px) {
  .kpi-card {
    flex: 0 0 50%;
    max-width: 50%;
  }

  .chart-card {
    flex: 0 0 100%;
    max-width: 100%;
  }

  .header-content {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .header-info {
    width: 100%;
    justify-content: space-between;
  }
}

@media (max-width: 480px) {
  .kpi-card {
    flex: 0 0 100%;
    max-width: 100%;
  }
}

.is-selected {
  color: #1989FA;
}
</style>

<style scoped>
.table-pagination-container {
  padding: 20px;
}

.pagination-container {
  margin-top: 15px;
  text-align: right;
}
</style>
